package cn.sc.allinpay.allfavour.business.saletrans;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import java.util.List;

import cn.pai.common.listener.OnPromptListener;
import cn.pai.mvp.intervenor.Intervenor;
import cn.pai.mvp.presenter.PaiPresenter;
import cn.sc.allinpay.allfavour.business.common.data.SaleCouponRecord;
import cn.sc.allinpay.allfavour.module.device.DeviceLogin;
import cn.sc.allinpay.allfavour.module.printer.IPrinter;
import cn.sc.allinpay.allfavour.module.printer.PrintData;
import cn.sc.allinpay.allfavour.module.printer.PrintManager;

/**
 * 售券流水 presenter
 * author：pany
 * on 2017/12/1 11:27
 */
public class SaleTransPresenter extends PaiPresenter<SaleTransContract.View> implements SaleTransContract.Presenter {

    public static final int REQ_TRANS_DETAIL = 0x01;

    private SaleTransModel model = new SaleTransModel();
    private boolean isQuerying = false;//是否正在查询，防止spinner引起的问题
    private IPrinter printer;


    @Override
    protected Intervenor createIntervenor() {
        return new Intervenor() {
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
            }

            @Override
            public void onResume() {
                super.onResume();
                DeviceLogin.login(view.getContext());
            }

            @Override
            public void onPause() {
                super.onPause();
                DeviceLogin.logout();
            }

            @Override
            public void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                if (Activity.RESULT_OK == resultCode) {
                    if (REQ_TRANS_DETAIL == requestCode) {
                        queryTransRecords();
                    }
                }
            }
        };
    }

    @Override
    public void start() {
        queryTransRecords();
    }

    @Override
    public void queryTransRecords() {
        if (!isQuerying) {
            isQuerying = true;
            //0：今日 1：所有
            view.loading("数据查询中...");
            model.queryTransRecord(1, new SaleTransModel.TransCallBack() {
                @Override
                public void onResult(List<SaleCouponRecord> resp, int size) {
                    isQuerying = false;
                    view.loaded();
                    if (resp != null && !resp.isEmpty()) {
                        view.showPromt(View.GONE, null);
                        view.transAdapterChange(resp);
                    } else {
                        view.transAdapterClean();
                        view.showPromt(View.VISIBLE, "未找到相关流水");
                    }
                }
            });
        }
    }

    public void print(final SaleCouponRecord record) {
        //因为onActivityResult回来没有立即执行onResume方法
        DeviceLogin.login(view.getContext());
        view.loading("售券成功，正在打印...");
        PrintData data = new PrintData();
        data.setMerchantName(model.getGlobal().getConfigData().getMerName());
        data.setTerminalId(model.getGlobal().getConfigData().getTerId());
        data.setOperNo(record.getOperator());
        data.setOperName(record.getOperatorName());
        data.setTime(record.getTransDate()+record.getTransTime());
        data.setTransName("售券");
        data.setCouponSeqNum(record.getSeqNum());
        data.setCouponName(record.getCouponName());
        data.setCouponType(record.getCouponType());
        data.setCouponUseNum(record.getCouponNum());
        data.setCouponPrice(record.getCouponPrice());
        data.setCouponUrl(record.getDesc());
        printer = PrintManager.createPrinter(PrintManager.PRINT_SALE, data);
        printer.print(new IPrinter.OnPrintListener() {
            @Override
            public void onStart(int step) {

            }

            @Override
            public void onSuccess(int step) {
                view.loaded();
                view.prompt(false, "打印成功", new OnPromptListener("确定") {
                    @Override
                    public void onPositive(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        view.setResult(Activity.RESULT_OK, null);
                        view.finish();
                    }
                });
            }

            @Override
            public void onFail(int code, String msg) {
                view.loaded();
                view.prompt(false, msg + ",是否继续打印", new OnPromptListener("确定", "取消") {
                    @Override
                    public void onPositive(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        print(record);
                    }

                    @Override
                    public void onNegative(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        view.setResult(Activity.RESULT_OK, null);
                        view.finish();
                    }
                });
            }
        });
    }
}
